//
//  MainMapViewController.m
//  CooperLinker
//
//  Created by 张 晓军 on 16/4/10.
//  Copyright © 2016年 洪雯. All rights reserved.


//    AMapCloudPOIAroundSearchRequest *request1 = [[AMapCloudPOIAroundSearchRequest alloc] init];
//    request1.tableID = @"56fcae27305a2a32884e8a5a";//在数据管理台中取得
//    request1.center = [AMapGeoPoint locationWithLatitude:39.5833 longitude:118.18506];
//    request1.radius = 5000;
//    request1.offset = 10;
//    request.keywords = @"园";
//    [self.delaget.search AMapCloudPOIAroundSearch: request1];



//
//            得到图片地址
//            if (arryInfo.images.count) {
//                AMapCloudImage *image = arryInfo.images[0];
//                NSLog(@"===%@",image.preurl);
//            }



#import "MainMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
//#import "AppDelegate.h"
#import "TabBarViewController.h"
//#import "CustomAnnotationView.h"
#import "MyMAPinAnnotationView.h"
#import "AMapViewController.h"
#import "ShowMapViewController.h"
#import "UIImageView+WebCache.h"
#import "DorpView.h"



#define WINSIZE [[UIScreen mainScreen] bounds]
#define BackMapHight 40

#define IMageMap @"20140614.jpg" //背景图
#define DaTouZhenMark @"2010" //mark
#define KeyWordMark @"2011" //keyWord 的mark

#define Zool1Min 13
#define Zool1Max 17

#define Zool2Min 17
#define Zool2Max 18

#define Zool3Min 18
#define Zool3Max 19

#define colorName  @[@"红",@"绿",@"黄",@"红",@"红",@"蓝"]

#define Form1 @"5710a3177bbf1959a515de4c"
#define Form2 @"571114fe7bbf1959a515ec00"
#define Form3 @"5711e50e7bbf1959a515fcfd"
#define ShowButtonTag 2000
#define ButtonZool 1  //每次zool增大和减小的数
@interface MainMapViewController ()<MAMapViewDelegate,AMapSearchDelegate,screenDelaget>
{
    int _n;
    float _zoomLevel;
    BOOL _isPark;
    int _num;
    
    //当前的所显示
    NSMutableArray *_FristMap;
    NSMutableArray *_annotionFrist;
    
    NSMutableArray *_secondMap;
    NSMutableArray *_annotionSecond;
    
    NSMutableArray *_threadMap;
    NSMutableArray *_annotionThread;
    
    NSMutableArray *_keyWordArry;
    NSMutableArray *_annotinKeyWord;
    
    NSString *_keyWord; //关键字
    BOOL _FlagkeyWord;
    
    DorpView *_dorpView;
}

@property (nonatomic, strong)MAMapView *mapView;
@property (strong, nonatomic) AMapSearchAPI *search;
@property (strong, nonatomic) MAUserLocation *userLocation;
@end

@implementation MainMapViewController
- (void)PopViewController {
    [self userMapInfo];
    [self showMap];
    [self setMark];
    [self creatSeachUI];

}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    [self.mapView setZoomLevel:16 animated:NO];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UIButton *btn = (UIButton *)[self.view viewWithTag:ShowButtonTag];
    btn.selected = NO;
    btn.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initData];
    [self showMap];
    [self userMapInfo];
    [self setMark];
    [self creatSeachUI];
    [self backButton];
    
    
}

//*************  初始化mark的三级存储     *************************************
- (void)initData {
    _annotionFrist = [[NSMutableArray alloc] init];
    _annotionSecond = [[NSMutableArray alloc] init];
    _annotionThread = [[NSMutableArray alloc] init];
    
    _FristMap = [[NSMutableArray alloc] init];
    _secondMap = [[NSMutableArray alloc] init];
    _threadMap = [[NSMutableArray alloc] init];
    
    _keyWordArry = [[NSMutableArray alloc] init];

}

//*************  初始化用户在地图的相关信息  *************************************
- (void)userMapInfo {
    
    //判断是否在园区，并且初始化坐标位置
    [self judgIsGard];
}

- (void)judgIsGard {
    if ((self.mapView.userLocation.coordinate.latitude > 39.6426940000 && self.mapView.userLocation.coordinate.latitude <39.6441270000) && (self.mapView.userLocation.coordinate.longitude >117.9565930000  && self.mapView.userLocation.coordinate.longitude < 117.9602330000)) {
        [self.mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES]; //地图跟着位置移动
        _isPark = YES;
    }else {
        self.mapView.customizeUserLocationAccuracyCircleRepresentation = NO;
        self.mapView.centerCoordinate = CLLocationCoordinate2DMake(39.60329,118.16286);
        _isPark = NO;
    }
}
//*************     展示地图          *************************************
- (void)showMap {
    
    [self initMap];
    [self creatMap];
    [self removeLogo];
    [self getMapIMage];
    [self creatButton];
}


- (void)initMap {
    [MAMapServices sharedServices].apiKey= @"b499b5039cf52f06eca89a6c56cd1a00";
    [AMapSearchServices sharedServices].apiKey = @"b499b5039cf52f06eca89a6c56cd1a00";
}

- (void)creatMap {
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, WINSIZE.size.width, WINSIZE.size.height - BackMapHight)];
    self.mapView.showsLabels = NO;
    self.mapView.touchPOIEnabled = NO;
    self.mapView.showsUserLocation = YES;    //YES 为打开定位，NO为关闭定位
    self.mapView.delegate = self;
//    _mapView.centerCoordinate = CLLocationCoordinate2DMake(39.5833,118.18506);
    [self.view addSubview:self.mapView];
    
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;

    
}


- (void)removeLogo {
    

}
//*************     设立按钮          *************************************
- (void)creatButton {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height - 60 - BackMapHight, 35, 35)];
    //    btn.backgroundColor = [UIColor yellowColor];
    [btn setBackgroundImage:[UIImage imageNamed:@"未标题-1"] forState:UIControlStateNormal];
    //    [btn setTitle:@"切换视角" forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"未标题-1"] forState:UIControlStateSelected];
    btn.tag = ShowButtonTag;
    btn.titleLabel.font = [UIFont systemFontOfSize:9];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(ClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    NSArray *arry= @[@"+",@"-"];
    for (int i = 0; i < 2; i++) {
        UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(WINSIZE.size.width - 50, WINSIZE.size.height - 120 - BackMapHight + 40*i, 40, 40)];
        addBtn.backgroundColor = [UIColor yellowColor];
        addBtn.tag = 200 +i;
        [addBtn setTitle:arry[i] forState:UIControlStateNormal];
        [addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [addBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [addBtn addTarget:self action:@selector(ClickAddBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:addBtn];
        if (i == 0) {
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(WINSIZE.size.width - 48, WINSIZE.size.height - 81 - BackMapHight, 36, 2)];
            lineView.backgroundColor = [UIColor grayColor];
            [self.view addSubview:lineView];
        }
    }
   
    
    UIButton *showButton = [[UIButton alloc] initWithFrame:CGRectMake(WINSIZE.size.width -50, WINSIZE.size.height - 170 - BackMapHight, 40, 40)];
    showButton.backgroundColor = [UIColor yellowColor];
    [showButton setTitle:@"实景" forState:UIControlStateNormal];
    [showButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [showButton addTarget:self action:@selector(showButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:showButton];
}

- (void)backButton {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.center.x - 20, WINSIZE.size.height - 40, 40, 40)];
    backButton.backgroundColor = [UIColor orangeColor];
    [backButton.layer setMasksToBounds:YES];
    backButton.layer.cornerRadius = 20;
    [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

- (void)backClick {
    [self.mapView removeAnnotations:_annotionFrist];
    [self.mapView removeAnnotations:_annotionSecond];
    [self.mapView removeAnnotations:_annotionThread];
    [self.mapView removeFromSuperview];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)showButton {
    ShowMapViewController *mapView = [[ShowMapViewController alloc] init];
    [self.navigationController pushViewController:mapView animated:YES];
}

//*************     设立标示点         *************************************
- (void)setMark {
    
    //构造AMapCloudPOILocalSearchRequest对象，设置本地检索请求参数
//    AMapCloudPOILocalSearchRequest *request = [[AMapCloudPOILocalSearchRequest alloc] init];
//    request.tableID = @"56fcae27305a2a32884e8a5a";//在数据管理台中取得
//    request.keywords = @"服务区";
//    request.city = @"唐山";
//    request.offset = 100;
//    //发起云本地检索
//    [self.delaget.search AMapCloudPOILocalSearch: request];
    if (self.uuid > 0) {
        AMapCloudPOIIDSearchRequest  *request = [[AMapCloudPOIIDSearchRequest alloc] init];
        request.tableID = Form3;
        request.uid = self.uuid;
        _FlagkeyWord = ! _FlagkeyWord;
        [self.search AMapCloudPOIIDSearch:request];
    }

}

//*************     设立查询按钮         *************************************
- (void)creatSeachUI {
    //搜索框
    UIButton *titlebtn = [[UIButton alloc] initWithFrame:CGRectMake(10,22, WINSIZE.size.width - 60, 40)];
    [titlebtn addTarget:self action:@selector(ClickSeachBtn) forControlEvents:UIControlEventTouchUpInside];
    titlebtn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:titlebtn];
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, WINSIZE.size.width - 90, 40)];
    titleLable.backgroundColor = [UIColor yellowColor];
    [titlebtn addSubview:titleLable];
    UIButton *btnDropbox = [[UIButton alloc] initWithFrame:CGRectMake(WINSIZE.size.width -50, 22, 40, 40)];
    btnDropbox.backgroundColor = [UIColor orangeColor];
    [btnDropbox addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnDropbox];
    
    
}
#pragma mark 点击查询
- (void)ClickSeachBtn {
    AMapViewController *amapV = [[AMapViewController alloc] init];
    [self presentViewController:amapV animated:NO completion:nil];
}

- (void)selectBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        _dorpView= [[DorpView alloc] initWithFrame:CGRectMake(0, 62, WINSIZE.size.width, 0)];
        _dorpView.screenDelaget = self;
        [self.view addSubview:_dorpView];
        [_dorpView starAnnotion];
    }else {
        [_dorpView stopAnnotion];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark 点击视角
- (void)ClickBtn:(UIButton *)sender {
    
    if (_isPark) {
        [self.mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES]; //地图跟着位置移动
        
    }
    sender.selected = YES;
    sender.backgroundColor = [UIColor yellowColor];
    _n ++;
    if (_n == 1) {
        [self.mapView setCameraDegree:60];
        return;
    }
    
    if (_n == 2) {
        [self.mapView setCameraDegree:0];
        _n = 0;
        return;
    }
}
#pragma mark 点击加号和减号
- (void)ClickAddBtn:(UIButton *)sender {
    
    _zoomLevel = self.mapView.zoomLevel;
    if (sender.tag == 200) {
        if (_zoomLevel<18) {
            UIButton *btn = (UIButton *)[self.view viewWithTag:201];
            btn.backgroundColor = [UIColor yellowColor];
            UIButton *btnC = (UIButton *)[self.view viewWithTag:200];
            btnC.backgroundColor = [UIColor yellowColor];
            [self.mapView setZoomLevel:_zoomLevel + ButtonZool animated:NO];
            if (_zoomLevel + ButtonZool >= 18) {
                sender.backgroundColor = [UIColor whiteColor];
            }
            
        }
    }
    if (sender.tag == 201) {
        if (_zoomLevel >12) {
            UIButton *btn = (UIButton *)[self.view viewWithTag:200];
            btn.backgroundColor = [UIColor yellowColor];
            UIButton *btnC = (UIButton *)[self.view viewWithTag:201];
            btnC.backgroundColor = [UIColor yellowColor];
            [self.mapView setZoomLevel:_zoomLevel -ButtonZool animated:NO];
            if (_zoomLevel - ButtonZool <= 12) {
                sender.backgroundColor = [UIColor whiteColor];
            }
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/







//----------------------------- 地图的一些回调和涂层的设置----------------------------------


#pragma mark 获得用户当前的位置并存储起来，判断是否越界
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    
    self.userLocation = userLocation;
    if(updatingLocation)
    {
        //取出当前位置的坐标
        //        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}
//-------------------- 普图
- (void)getMapIMage {
    
    MACoordinateBounds coordinateBounds = MACoordinateBoundsMake(CLLocationCoordinate2DMake
                                                                 (39.614169,118.145792),CLLocationCoordinate2DMake(39.5833,118.18506));
    
    MAGroundOverlay *groundOverlay = [MAGroundOverlay groundOverlayWithBounds:coordinateBounds icon:[UIImage imageNamed:IMageMap]];
    
    [self.mapView addOverlay:groundOverlay];
    
    self.mapView.visibleMapRect = groundOverlay.boundingMapRect;
    
}

//-------------------- mark的样式
#pragma mark  mark的样式
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MyMAPinAnnotationView *annotationView = (MyMAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MyMAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
  //***mark的基本设置------------
        //第一步获得是哪个AMapCloudPOI
        AMapCloudPOI *amapCloudPoi = [self getWhichMapCloudPOI:annotation];
        if (_FlagkeyWord) {
            annotationView.backImageView.image = [UIImage imageNamed:@"2010"];
            _FlagkeyWord = !_FlagkeyWord;
        }else{
            annotationView.backImageView.image = [UIImage imageNamed:[self getMarkColor:amapCloudPoi]];
        }
        if (amapCloudPoi.images.count) {
            [self getMarkInfoImage:amapCloudPoi :annotationView.markInageView];
        }else{
            annotationView.markInageView.image = [UIImage imageNamed:@""];
        }

            annotationView.markLabel.text = amapCloudPoi.name;
            
            // 设置为NO，用以调用自定义的calloutView
            annotationView.canShowCallout = NO;
            // 设置中心点偏移，使得标注底部中间点成为经纬度对应点
            annotationView.centerOffset = CGPointMake(0, -18);
            
            annotationView.animatesDrop = YES;
            return annotationView;
    }
    return nil;
}


- (AMapCloudPOI *)getWhichMapCloudPOI:(id<MAAnnotation>)annotation {
    AMapCloudPOI *amapCloudPOI = [[AMapCloudPOI alloc] init];
    
    for (AMapCloudPOI *arryInfo in _keyWordArry) {
        if (annotation.coordinate.latitude == arryInfo.location.latitude && annotation.coordinate.longitude == arryInfo.location.longitude) {
            if (_FlagkeyWord) {
                amapCloudPOI = arryInfo;
                return amapCloudPOI;
            }else {
                _FlagkeyWord = !_FlagkeyWord;
                amapCloudPOI = arryInfo;
                return amapCloudPOI;
            }
            
        }
    }
    
    for (AMapCloudPOI *arryInfo in _FristMap) {
        if (annotation.coordinate.latitude == arryInfo.location.latitude && annotation.coordinate.longitude == arryInfo.location.longitude) {
            amapCloudPOI = arryInfo;
            return amapCloudPOI;
        }
    }
    
    for (AMapCloudPOI *arryInfo in _secondMap) {
        if (annotation.coordinate.latitude == arryInfo.location.latitude && annotation.coordinate.longitude == arryInfo.location.longitude) {
            amapCloudPOI = arryInfo;
            return amapCloudPOI;
        }
    }
    
    for (AMapCloudPOI *arryInfo in _threadMap) {
        if (annotation.coordinate.latitude == arryInfo.location.latitude && annotation.coordinate.longitude == arryInfo.location.longitude) {
            amapCloudPOI = arryInfo;
            return amapCloudPOI;
        }
    }

    return nil;
}

- (NSString *)getMarkColor:(AMapCloudPOI *)amapCloudPoi {
    NSDictionary *dic = amapCloudPoi.customFields;
    NSArray *arryInfo = [dic allKeys];
    for (NSString *key in arryInfo) {
        if ([key isEqualToString:@"leixing"]) {
            NSNumber *numLeiXing = dic[key];
            int num = [numLeiXing intValue];
            return colorName[num];
        }
    }
    return colorName[0];
}

- (void)getMarkInfoImage:(AMapCloudPOI *)amapCloudPoi :(UIImageView *)imageView{
    AMapCloudImage *imageUrl = amapCloudPoi.images[0];
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl.preurl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        imageView.image = image;
    }];
    
}

//实现<MAMapViewDelegate>协议中的mapView:viewForOverlay:回调函数，以在地图上显示图片覆盖物。
- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAGroundOverlay class]])
    {
        MAGroundOverlayView *groundOverlayView = [[MAGroundOverlayView alloc]
                                                  initWithGroundOverlay:overlay];
        
        return groundOverlayView;
    }
    return nil;
}
//----------------------
#pragma mark 地图将要发生移动时
- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction  {
    
//    if (self.mapView.zoomLevel >Zool1Min && self.mapView.zoomLevel <=Zool1Max ) {
//        AMapCloudPOIAroundSearchRequest *quest = [[AMapCloudPOIAroundSearchRequest alloc] init];
//        quest.center = [AMapGeoPoint locationWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude];
//        quest.tableID = Form1;
//        quest.offset = 20;
//        quest.keywords = _keyWord;
//        [self.search AMapCloudPOIAroundSearch:quest];
//        return;
//    }

    if (self.mapView.zoomLevel >Zool2Min && self.mapView.zoomLevel <=Zool2Max ) {
        AMapCloudPOIAroundSearchRequest *quest = [[AMapCloudPOIAroundSearchRequest alloc] init];
        quest.center = [AMapGeoPoint locationWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude];
        quest.tableID = Form2;
        quest.offset = 20;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }
    if (self.mapView.zoomLevel >Zool3Min && self.mapView.zoomLevel <=Zool3Max) {
        AMapCloudPOIAroundSearchRequest *quest = [[AMapCloudPOIAroundSearchRequest alloc] init];
        quest.center = [AMapGeoPoint locationWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude];
        quest.tableID = Form3;
        quest.offset = 20;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }

    
}


//----------------------
#pragma mark 地图缩放
- (void)mapView:(MAMapView *)mapView mapDidZoomByUser:(BOOL)wasUserAction {
    AMapCloudPOIAroundSearchRequest *quest = [[AMapCloudPOIAroundSearchRequest alloc] init];
    quest.center = [AMapGeoPoint locationWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude];
    
    if (self.mapView.zoomLevel > Zool1Min && self.mapView.zoomLevel <= Zool1Max) {
        quest.tableID = Form1;
        quest.radius = 3000;
        quest.offset = 35;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }
    if (self.mapView.zoomLevel >Zool2Min && self.mapView.zoomLevel <=Zool2Max ) {
        quest.tableID = Form2;
        quest.radius = 1000;
        quest.offset = 20;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }
    if (self.mapView.zoomLevel >Zool3Min && self.mapView.zoomLevel <=Zool3Max) {
        quest.tableID = Form3;
        quest.radius = 1000;
        quest.offset = 20;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }
 
}


//========================== 地图的查询回调  ==========================================
- (void)onCloudSearchDone:(AMapCloudSearchBaseRequest *)request response:(AMapCloudPOISearchResponse *)response {
//--有keyWord
    if (_FlagkeyWord) {
        NSLog(@"keyWord===>%lu",(unsigned long)_keyWordArry.count);
        AMapCloudPOI *arryInfo = response.POIs[0];
        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
        pointAnnotation.coordinate = CLLocationCoordinate2DMake(arryInfo.location.latitude, arryInfo.location.longitude);
        pointAnnotation.title = arryInfo.name;
        pointAnnotation.subtitle = arryInfo.address;
        [_annotinKeyWord addObject:pointAnnotation];
        [_keyWordArry addObject:arryInfo];
        [self.mapView addAnnotation:pointAnnotation];
        return;
    }
   
    
//--第一级别
    if (self.mapView.zoomLevel > Zool1Min && self.mapView.zoomLevel <= Zool1Max) {
        //删除上一级的大头针
        [self.mapView removeAnnotations:_annotionSecond];
        [self.mapView removeAnnotations:_annotionThread];
        [_secondMap removeAllObjects];
        [_threadMap removeAllObjects];
        [_annotionSecond removeAllObjects];
        [_annotionThread removeAllObjects];
        NSLog(@"Frist == >%lu",(unsigned long)_FristMap.count);
        for (AMapCloudPOI *arryInfo in response.POIs) {
             int flg = 0;
        //判断是否之前有，有的话删除
            for (int i = 0; i < _FristMap.count; i++) {
                AMapCloudPOI *arryInfoOld =  _FristMap[i];
                if (arryInfoOld.location.latitude == arryInfo.location.latitude && arryInfoOld.location.longitude == arryInfo.location.longitude) {
                    flg = 1;
                    break;
                }
            }
            
       //之前没有的把新的加进来
            if (flg == 0) {
                MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
                pointAnnotation.coordinate = CLLocationCoordinate2DMake(arryInfo.location.latitude, arryInfo.location.longitude);
                pointAnnotation.title = arryInfo.name;
                pointAnnotation.subtitle = arryInfo.address;
                [_annotionFrist addObject:pointAnnotation];
                [_FristMap addObject:arryInfo];
                [self.mapView addAnnotation:pointAnnotation];
            }
            
        }
        return;
    }
    
//--第二级别
    if (self.mapView.zoomLevel >Zool2Min && self.mapView.zoomLevel <=Zool2Max) {
        [self.mapView removeAnnotations:_annotionThread];
        [_threadMap removeAllObjects];
        [_annotionThread removeAllObjects];
        NSLog(@"2 == >%lu",(unsigned long)_secondMap.count);

        for (AMapCloudPOI *arryInfo in response.POIs) {
            int flg2 = 0;
            //判断是否之前有，有的话删除
            for (int i = 0; i < _secondMap.count; i++) {
                AMapCloudPOI *arryInfoOld =  _secondMap[i];
                if (arryInfoOld.location.latitude == arryInfo.location.latitude && arryInfoOld.location.longitude == arryInfo.location.longitude) {
                    flg2 = 1;
                    break;
                }
            }
            //之前没有的把新的加进来
            if (flg2 == 0) {
                MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
                pointAnnotation.coordinate = CLLocationCoordinate2DMake(arryInfo.location.latitude, arryInfo.location.longitude);
                pointAnnotation.title = arryInfo.name;
                pointAnnotation.subtitle = arryInfo.address;
                [_annotionSecond addObject:pointAnnotation];
                [_secondMap addObject:arryInfo];
                [self.mapView addAnnotation:pointAnnotation];
            }
            
        }
        return;
    }
    
//--第三级别
    if (self.mapView.zoomLevel >Zool3Min && self.mapView.zoomLevel <=Zool3Max) {
                    NSLog(@"3 == >%lu",(unsigned long)_threadMap.count);
        for (AMapCloudPOI *arryInfo in response.POIs) {
            int flg3 = 0;
            //判断是否之前有，有的话删除
            for (int i = 0; i < _threadMap.count; i++) {
                AMapCloudPOI *arryInfoOld =  _threadMap[i];
                if (arryInfoOld.location.latitude == arryInfo.location.latitude && arryInfoOld.location.longitude == arryInfo.location.longitude) {
                    flg3 = 1;
                    break;
                }
            }
            
            //之前没有的把新的加进来
            if (flg3 == 0) {
                MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
                pointAnnotation.coordinate = CLLocationCoordinate2DMake(arryInfo.location.latitude, arryInfo.location.longitude);
                pointAnnotation.title = arryInfo.name;
                pointAnnotation.subtitle = arryInfo.address;
                [_annotionThread addObject:pointAnnotation];
                [_threadMap addObject:arryInfo];
                [self.mapView addAnnotation:pointAnnotation];

            }
            
            
        }
        return;

    }
    
}

//========================== 检索  ==========================================
- (void)ChooseDelaget:(NSInteger)num {
    //获得关键字
    NSArray *arryInfo = @[@"0",@"1",@"2",@"3",@"4",@"5"];
    _keyWord = arryInfo[num];
    int newnun = (int)num;
    [self getSeachInfo:newnun];
}

- (void)getSeachInfo:(int)num {
    //删除所有界面上的图标
    [self.mapView removeAnnotations:_annotionFrist];
    [self.mapView removeAnnotations:_annotionSecond];
    [self.mapView removeAnnotations:_annotionThread];
    
    [_FristMap removeAllObjects];
    [_secondMap removeAllObjects];
    [_threadMap removeAllObjects];
    
    [_annotionFrist removeAllObjects];
    [_annotionSecond removeAllObjects];
    [_annotionThread removeAllObjects];
    //向高德发起查询
    [self GetGaoDeYunInfo];

}

- (void)GetGaoDeYunInfo {
    AMapCloudPOIAroundSearchRequest *quest = [[AMapCloudPOIAroundSearchRequest alloc] init];
    quest.center = [AMapGeoPoint locationWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude];
    
    if (self.mapView.zoomLevel > Zool1Min && self.mapView.zoomLevel <= Zool1Max) {
        quest.tableID = Form1;
        quest.radius = 3000;
        quest.offset = 100;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }
    if (self.mapView.zoomLevel >Zool2Min && self.mapView.zoomLevel <=Zool2Max ) {
        quest.tableID = Form2;
        quest.radius = 1000;
        quest.offset = 100;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }
    if (self.mapView.zoomLevel >Zool3Min && self.mapView.zoomLevel <=Zool3Max) {
        quest.tableID = Form3;
        quest.radius = 1000;
        quest.offset = 20;
        quest.keywords = _keyWord;
        [self.search AMapCloudPOIAroundSearch:quest];
        return;
    }

}



@end
